﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class AddBook_form : Form
    {
        public AddBook_form()
        {
            InitializeComponent();
        }

        private void AddBook_form_Load(object sender, EventArgs e)
        {
            //图书类型数据源
            string sql = "select * from BookType";
            DataTable dt = new DataTable();
            dt = DBhelper.GetDataTable(sql);
            comboBox1.DataSource= dt;
            comboBox1.ValueMember = "typeid";
            comboBox1.DisplayMember = "typename";

            //出版社数据源
            string sql2 = "select * from Press";
            DataTable dt2 = DBhelper.GetDataTable(sql2);
            comboBox2.DataSource= dt2;
            comboBox2.ValueMember = "pressid";
            comboBox2.DisplayMember = "pressname";

            //书架数据源
            string sql3 = "select * from Pressmark";
            DataTable dt3 = DBhelper.GetDataTable(sql3);
            comboBox3.DataSource= dt3;
            comboBox3.ValueMember = "pressmarkid";
            comboBox3.DisplayMember = "pressmarknumber";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string bookid = textBox1.Text.Trim();
            string bookname = textBox2.Text.Trim();
            string bookauthor = textBox3.Text.Trim();
            int typeid = int.Parse(comboBox1.SelectedValue.ToString());
            int pressid = int.Parse(comboBox2.SelectedValue.ToString());
            int pressmarkid = int.Parse(comboBox3.SelectedValue.ToString());
            int amount = Convert.ToInt32(numericUpDown1.Value);

            //经典
            if(bookid == "")
            {
                MessageBox.Show("图书条形码不能为空!");
                textBox1.Focus();
                return;
            }
            if (bookid.Length != 13)
            {
                MessageBox.Show("图书条形码不正确!");
                textBox1.Focus();
                return;
            }
            if (DBhelper.checkBook(bookid))
            {
                MessageBox.Show("这本书已经上架了!");
                textBox1.Focus();
                return;
            }
            if (bookname == "")
            {
                MessageBox.Show("图书名称不能为空");
                textBox1.Focus();
                return;
            }
            if (bookauthor == "")
            {
                MessageBox.Show("图书作者不能为空");
                textBox1.Focus();
                return;
            }

            DialogResult rs =
                MessageBox.Show(
                    $"条形码：{bookid}\r\n" +
                    $"图书名：{bookname}\r\n" +
                    $"图书作者：{bookauthor}\r\n" +
                    $"图书类型：{comboBox1.Text}\r\n" +
                    $"出版社：{comboBox2.Text}\r\n" +
                    $"书架：{comboBox3.Text}\r\n" +
                    $"数量：{numericUpDown1.Value}\r\n" +
                    $"确定要上架吗？","提示",MessageBoxButtons.YesNo);

            if(rs == DialogResult.Yes)
            {
                string sql = $"insert into BookInfo values('{bookid}','{bookname}','{typeid}','{bookauthor}','{pressid}','{pressmarkid}','{amount}',default)";
                if(DBhelper.NonQuery(sql))
                {
                    MessageBox.Show("上架成功！");
                }
                else
                {
                    MessageBox.Show("上架失败");
                }
            }
        }
    }
}
